$(function() {
	var dateBaseUrl = "http://120.77.74.226/";
//	dateBaseUrl = "http://yinby.iok.la/";
	//dateBaseUrl = "http://127.0.0.1";
	var requestHerfArray = ["queryReportNumber", "queryReportEmotion", "queryReportHotWord", "queryReportTopic", "queryReportKeyWord", "queryReportNumberTrend", "queryReportEmotionTrend"];
	var index = 0;
	var load = new Loading();
	
	requestTopicList();
	initData();

	$(".bar span").click(function() {
		$(this).addClass("active");
		$(this).siblings().removeClass("active");
		initData();
	})
	
	$(".bar select").change(function(){
		initData();
	})
	
	function initData(){
		load = new Loading();
		load.init();
	   	load.start();
	   	index = 0;
		requestData(requestHerfArray[index]);
	}
	
	function requestTopicList() {
		$.ajax({
			url: dateBaseUrl + "prophetController.do?topicList",
			type: "post",
			dataType: "json",
			timeout : 1000, 
			success: function(data) {
				var resultdataNew = eval(data);
				if(resultdataNew.success) {
					$(".bar select").empty();
					$(".bar select").append("<option value='0'>全部</option>");
					for(var i = 0; i < resultdataNew.result.length; i++) {
						$(".bar select").append("<option value='"+resultdataNew.result[i].id+"'>"+resultdataNew.result[i].name+"</option>")
					}
				}
			}
		})
	}

	function requestData(flag) {
		var dayParam = $(".bar span.active").attr("data-param");
		var keyWordTopicId = $(".bar select").val();
		if(index >= requestHerfArray.length){
			load.stop();
			return;
		}
		$.ajax({
			type: "post",
			dataType: "json",
			timeout : 1000,
			data: { "time": getToday(dayParam), "timeType": 0, "keyWordTopicId": keyWordTopicId },
			url: dateBaseUrl + "homePageController.do?" + flag,
			success: function(data) {
				if(data.success) {
					pushData(data, flag);
				} else {
					$(".container").eq(index).hide();
					index++;
					requestData(requestHerfArray[index]);
				}
			},
			complete:function(data){
				if(data.statusText=='timeout'){//超时,status还有success,error等值的情况
					$(".container").eq(index).hide();
					index++;
					requestData(requestHerfArray[index]);
				}
			}
		});
	}
	
	function pushData(data, flag) {
		switch(index) {
			case 0:
				var sourceName = arrayList(data.result, "sourceName", false);
				var dataNumber = arrayList(data.result, "value", false);
				zhuzhuang("", sourceName, "舆情数量", dataNumber, false, "sourceStationType", function() {
					index++;
					requestData(requestHerfArray[index]);
				});
				break;
			case 1:
				gaugeD(data.result.value, "emotionAnalysisId", function() {
					index++;
					requestData(requestHerfArray[index]);
				});
				break;
			case 2:
				$("#hotWordId").empty();
				$("#hotWordId").jQCloud(arrayListCloud(data.result.listData));
				index++;
				requestData(requestHerfArray[index]);
				break;
			case 3:
				zhuzhuang("", [data.result.logDate], "舆情数量", [data.result.value], false, "toPicId", function() {
					index++;
					requestData(requestHerfArray[index]);
				});
				break;
			case 4:
				var sourceName = arrayListKeyWord(data.result.listData, "name", false);
				var dataNumber = arrayListKeyWord(data.result.listData, "count", false);
				guanjiantop10("", sourceName, "舆情数量", dataNumber, false, "keyWordId", function() {
					index++;
					requestData(requestHerfArray[index]);
				});
				break;
			case 5:
				var logDate = arrayListHour(data.result, "hour", false);
				var value = arrayList(data.result, "value", false);
				qushi("", logDate, "舆情数量", value, false, "numberTrendId", function() {
					index++;
					requestData(requestHerfArray[index]);
				});
				break;
			case 6:
				var logDate = arrayListHour(data.result, "hour", false);
				var value = arrayList(data.result, "value", false);
				qushi("", logDate, "情感值", value, false, "emotionTrendId", function() {
					index++;
					requestData(requestHerfArray[index]);
				});
				break;
		}
	}
})

function getToday(param) {
	var today = new Date();
	today.setDate(today.getDate() - param);
	var year = today.getFullYear();
	var month = today.getMonth() + 1;
	var day = today.getDate();
	if(parseInt(month) < 10) {
		month = "0" + month;
	}
	if(parseInt(day) < 10) {
		day = "0" + day;
	}
	return year + "" + month + "" + day;
}

function arrayList(data, key, isXorY) {
	var arraylist = new Array();
	if(data != null) {
		for(var i = 0; i < data.length; i++) {
			if(isXorY) {
				if(data[i][key].length > 10) {
					arraylist.push(data[i][key].substring(0, 8) + "....");
				}
			} else {
				arraylist.push(data[i][key]);
			}
		};
	}
	return arraylist;
}

/**
 *  jqcloud
 * @param {Object} data
 */
function arrayListCloud(data) {
	var arraylist = new Array();
	if(data != null) {
		for(var i = 0; i < data.length; i++) {
			var json = {};
			json.text = data[i]['name'];
			json.weight = data[i][Math.round(Math.random() * 30)];
			arraylist[i] = json;
			if(i > 50) {
				break;
			}
		};
	}
	return arraylist;
}

/**
 *  
 * @param {Object} data 要解析的数据，根据数据里的key 转换的格式['新闻','贴吧'] 或者[1,200]
 * @param {Object} key  需要取得key的名字
 * @param {Object} 是否是y或者x轴的汉字，如果是汉字就截取8个后面加上...
 */
function arrayListKeyWord(data, key, isXorY) {
	var arraylist = new Array();
	var index = data.length > 9 ? 9 : data.length;
	for(var i = 0; i < index; i++) {
		if(isXorY) {
			if(data[i][key].length > 10) {
				arraylist.push(data[i][key].substring(0, 8) + "....");
			}
		} else {
			arraylist.push(data[i][key]);
		}

	};
	return arraylist;

}

/**
 *  
 * @param {Object} data 要解析的数据，根据数据里的key 转换的格式['新闻','贴吧'] 或者[1,200]
 * @param {Object} key  需要取得key的名字
 * @param {Object} 是否是y或者x轴的汉字，如果是汉字就截取8个后面加上...
 */
function arrayListHour(data, key, isXorY) {
	var arraylist = new Array();
	for(var i = 0; i < data.length; i++) {
		if(isXorY) {
			if(data[i][key].length > 10) {
				arraylist.push(data[i][key].substring(0, 8) + "....");
			}
		} else {
			arraylist.push(data[i][key] + ":00");
		}

	};
	return arraylist;

}